<?php
class Expositions extends Controller {

	function Expositions()
	{
		parent::Controller();	
		$this->load->helper('form');
		$this->load->model('Exposition_model', 'Exposition');
		$this->load->model('Rooms_model', 'Rooms');
		$this->load->model('Piece_model', 'Pieces');
		
	}
	
	
	// Affiche la liste des expositions
	function index()
	{
		
		$data['query_expositions'] = $this->db->get('expositions');
		foreach($data['query_expositions']->result() as $row){
			$t[$row->id] = $this->Pieces->get_one($row->id)->row()->id;
		}
		$data['t']=$t;
		$this->load->view('layout/header');
		$this->load->view('liste_expo_view', $data);
		$this->load->view('layout/footer');
			
	}
	
	// Affiche la grille des salles de l'expo contenant l'oeuvre d'id $piece_id
	function view($piece_id)
	{		
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		else{
		$query = $this->Pieces->get_expo_from_piece($piece_id);
		$expo_id=$query->row()->expo_id;
		$data['query_rooms'] = $this->Rooms->get_rooms($expo_id);	
		$this->load->view('expositions_view',$data);
		}
	}
	// Affiche la page d'accueil de gestion : liste des oeuvres et des salles de l'expo du respo loggu�
	function owner_home()
	{
		
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		else{

			$user_id = $this->tank_auth->get_user_id();
			$expo_id = $this->Exposition->get_expo_from_user($user_id);
			// On regarde si l'utilisateur a une expo ou non
			if($expo_id != (-1)){
		
				$data['query_exposition'] = $this->Exposition->get($expo_id);
				$data['query_rooms'] = $this->Rooms->get_rooms($expo_id);
				$data['query_pieces'] = $this->Pieces->get_all($expo_id);
		
				$this->load->view('layout/header_admin');
				$this->load->view('owner_expo_view',$data);
				$this->load->view('layout/footer');
	
			}
			else{
				redirect('expositions/add');
			}
		}
	
	}
	
	// Function de cr�ation d'une nouvelle exposition : affiche un formulaire vide
	function add()
	{
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		else{
	
		$data['owner_id']=$this->tank_auth->get_user_id();
		
		$this->load->view('layout/header_admin');
		$this->load->view('add_exposition_view',$data);
		$this->load->view('layout/footer');
		}	
	}
	
	// Fonction permettant d'�diter l'exposition de l'utilisateur �ventuellement logg�
	function edit()
	{
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		else{
		$user_id = $this->tank_auth->get_user_id();
		$expo_id = $this->Exposition->get_expo_from_user($user_id);
		
		$data['query_user_exposition'] = $this->Exposition->get($expo_id);
		
		$this->load->view('layout/header_admin');
		$this->load->view('edit_user_exposition_view',$data);
		$this->load->view('layout/footer');
		}
	}
	
	/*Fonction mettant � jour la BDD une fois l'�dition termin�e
	 * ou ins�rant un nouvel enregistrement dans le cas d'une cr�ation
	 */
	function confirm_changes()
	{
		// si l'expo existait d�j�, on update les tables expositions et addresses
		if (($_POST['id'])>0){
		$this->db->where('id',$_POST['id']);
		$this->db->update('expositions', array('title'=>$_POST['title'], 'description'=>$_POST['description'], 'start_date'=>$_POST['start_date'], 'end_date'=>$_POST['end_date']));
		
		$this->db->where('id', $_POST['address_id']);
		$this->db->update('addresses', array('zip_code'=> $_POST['zip_code'], 'city'=>$_POST['city'], 'address'=>$_POST['address'], 'place'=>$_POST['place']));
		}
		else{
		$this->db->insert('addresses', array('zip_code'=> $_POST['zip_code'], 'city'=>$_POST['city'], 'address'=>$_POST['address'], 'place'=>$_POST['place']));
		$address_id = $this->db->insert_id();
		$this->db->insert('expositions', array('address_id'=>$address_id,'owner_id'=>$_POST['owner_id'],'title'=>$_POST['title'], 'description'=>$_POST['description'], 'start_date'=>$_POST['start_date'], 'end_date'=>$_POST['end_date']));
		$expo_id = $this->db->insert_id();
		$room_id = $this->Rooms->add_room($expo_id,6,4);
		$this->db->where('id',$room_id);
		$this->db->update('rooms', array('name'=>'cave', 'is_private'=>'TRUE'));			
		}
				
		redirect("expositions/owner_home/");
	}
}
